﻿namespace Practices.IdentityProvider {
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;

    public class IdentityHelper {
        private static bool IsLocalUrl(string url) {
            return !string.IsNullOrEmpty(url) && ((url[0] == '/' && (url.Length == 1 || (url[1] != '/' && url[1] != '\\'))) || (url.Length > 1 && url[0] == '~' && url[1] == '/'));
        }

        public static void RedirectToReturnUrl(string returnUrl, HttpResponse response) {
            if (!String.IsNullOrEmpty(returnUrl) && IsLocalUrl(returnUrl)) {
                response.Redirect(returnUrl);
            } else {
                response.Redirect("~/");
            }
        }

        public static bool ValidateHostAddress(string hostAddress) {
            string hosts = ConfigurationManager.AppSettings["WebSealHosts"];
            var addresses = hosts.ToLower().Split(new char[] { ',' });
            foreach (string address in addresses) {
                if (address.Equals(hostAddress)) {
                    return true;
                }
            }
            return false;
        }

        public static bool ValidateCertificate(HttpClientCertificate certificate) {
            //检查证书是否有效以及主题是否正确
            if (certificate.IsPresent && certificate.IsValid) {
                //分析证书主题
                string[] fileds = certificate.Subject.Split(',');
                Dictionary<string, string> dics = new Dictionary<string, string>();
                foreach (string item in fileds) {
                    string[] kv = item.Trim().Split('=');
                    if (kv.Length != 2) continue;
                    dics.Add(kv[0], kv[1]);
                }
                if (dics.ContainsKey("CN") && dics.ContainsKey("SERIALNUMBER")) {
                    //读取配置
                    string cerSubjectCN = ConfigurationManager.AppSettings["WebSealCerSubjectCN"];
                    string cerSubjectSerIALNumbers = ConfigurationManager.AppSettings["WebSealCerSubjectNumbers"];
                    //if (string.IsNullOrEmpty(cerSubjectCN)) cerSubjectCN = "webseal.kc.cnpc";
                    //if (string.IsNullOrEmpty(cerSubjectSerIALNumbers)) cerSubjectSerIALNumbers = "W000000058,W000000059,W000000060";

                    if (dics["CN"].Equals(cerSubjectCN)) {
                        string[] numbers = cerSubjectSerIALNumbers.Split(',');
                        foreach (string n in numbers) {
                            if (dics["SERIALNUMBER"].Equals(n)) {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

        public static string ReplyToAddress(string replyToAddress) {
            if (replyToAddress.IndexOf("/adfs/services/trust") > -1) {
                replyToAddress = replyToAddress.Replace("http://", "https://");
                replyToAddress = replyToAddress.Replace("/adfs/services/trust", "/adfs/ls");                
            }
            return replyToAddress;
        }
    }
}